package com.example.administrator.tiyuapp.EventFragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.example.administrator.tiyuapp.Constants;
import com.example.administrator.tiyuapp.R;

import java.util.List;

/**
 * Created by Administrator on 2017/1/17.
 */
public class EventAdapter extends BaseAdapter {

    private Context context;
    private List<EventBean.DataBean.ContestListBean>  data;
    private Drawable drawableone;

    public EventAdapter(Context context, List<EventBean.DataBean.ContestListBean> data) {
        this.context = context;
        /*
        * dfghitgvfcfvgh
        * */
        this.data = data;
    }

    @Override
    public int getCount() {
        return data!=null?data.size():0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.event_item,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.name_one.setText(data.get(position).getContest().getH_t().getName());
        Log.e("imagelala", "onResponse: "+Constants.IMAGE_URL+data.get(position).getContest().getH_t().getLogo() );

        ImageRequest imageRequest = new ImageRequest(Constants.IMAGE_URL+data.get(position).getContest().getH_t().getLogo(), new Response.Listener<Bitmap>() {
            @Override

            public void onResponse(Bitmap response) {

                drawableone = new BitmapDrawable(response);


            }
        }, 100, 100, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        viewHolder.name_one.setCompoundDrawablesWithIntrinsicBounds(null,drawableone, null, null);
        viewHolder.name_two.setText(data.get(position).getContest().getA_t().getName());

        return convertView;
    }


    class ViewHolder {

        private TextView name_one,name_two;

        public ViewHolder(View convertView) {
            name_one = (TextView) convertView.findViewById(R.id.name_one);
            name_two = (TextView) convertView.findViewById(R.id.name_two);

        }
    }
}
